package com.hzx.demo.helper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.util.Log;

import com.hzx.demo.util.HZXLog;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 照相辅助类
 * 取名Module, 用于尊重Camera2的原作者
 * Created by ldkxingzhe on 2016/6/28.
 */
public class PhotoModule extends CameraModule implements Camera.PictureCallback, Camera.ShutterCallback {
    @SuppressWarnings("unused")
    private static final String TAG = "PhotoModule";
    private String mFileName;

    public void takePicture(String fileName){
        if(mCamera == null)
            throw new IllegalStateException("openCamera must be called before takePicture");

        makeSureFileFine(fileName);
        mFileName = fileName;
        mCamera.takePicture(this, null, this);
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        HZXLog.v(TAG, "onPictureTaken, and data.length is %d ", data.length);
        mCamera.startPreview();
        // TODO:考虑使用多线程，　避免拍照后的有卡顿现象
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        File outFile = new File(mFileName);
        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outFile));
            ExifInterface exif = new ExifInterface(mFileName);
            if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6")){
                bitmap= rotate(bitmap, 90);
            } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8")){
                bitmap= rotate(bitmap, 270);
            } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3")){
                bitmap= rotate(bitmap, 180);
            } else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("0")){
                bitmap= rotate(bitmap, 90);
            }
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    private Bitmap rotate(Bitmap bitmap, int degree) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        Matrix mtx = new Matrix();
        //  mtx.postRotate(degree);
        mtx.setRotate(degree);

        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

    @Override
    protected void diyCameraParameters(Camera.Parameters parameters) {
        List<String> focusModes = parameters.getSupportedFocusModes();
        if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)){
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }
    }

    @Override
    public void onShutter() {
        // 快门声添加
        // TODO
    }
}
